<template>
  <div class="table-page-search-wrapper">
    <a-card>
      <a-form layout="vertical">
        <a-row :gutter="48">
          <a-col :md="8" :sm="12">
            <a-form-item :label="$t('key.xn')" >
              <a-select allowClear v-model="queryParam.ksxn">
                <a-select-option v-for="i in TYPE0125" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <template v-if="advanced">
            <a-col :md="8" :sm="12">
              <a-form-item :label="$t('key.xq')" >
                <a-select allowClear v-model="queryParam.ksxq">
                  <a-select-option v-for="i in TYPE0072" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item :label="$t('key.kslb')" >
                <a-select allowClear v-model="queryParam.kslb">
                  <a-select-option v-for="i in TYPE0109" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item :label="$t('key.ksxz')" >
                <a-select allowClear v-model="queryParam.ksxz">
                  <a-select-option v-for="i in TYPE0358" :key="i.id" :value="i.code">{{ i.name }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="12">
              <a-form-item :label="$t('key.yx')">
                <a-select v-model="queryParam.kcyx" @change="yxChange" :filterOption="filterOption" allowClear showSearch>
                  <a-select-option v-for="i in selectMenu.yx" :key="i.id" :value="i.dwh">{{ i.dwh+'-'+i.dwmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>

            <a-col :md="8" :sm="12">
              <a-form-item :label="$t('key.kcmc')" >
                <a-select v-model="queryParam.kcdm" @change="kcChange" :filterOption="filterOption" allowClear showSearch>
                  <a-spin v-if="kcLoading" slot="notFoundContent" size="small"/>
                  <a-select-option v-for="(i, index) in selectMenu.courseList" :key="index" :value="i.kch">{{ i.kch + '-' + i.kcmc }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
          </template>

          <a-col :md="12" :sm="24">
            <span class="table-page-search-submitButtons" >
              <a-button type="primary" @click="handleQuery" style="margin:0 10px">{{ $t('button.query') }}</a-button>
              <a-button type="primary" @click="handlePrint" style="margin:0 10px">{{ $t('button.PrintCourse') }}</a-button>
              <a-button type="primary" @click="handlePrint2" style="margin:0 10px">{{ $t('button.Mergeprint') }}</a-button>
              <a-button style="margin: 0 10px" @click="() => queryParam = {}">{{ $t('button.reset') }}</a-button>
              <a @click="advanced = !advanced" style="margin-left: 8px">
                {{ advanced ? $t('button.packUp') : $t('button.advance') }}
                <a-icon :type="advanced ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </a-card>
    
    <a-card :bordered="false" style="margin-top: 10px">
      <s-table
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowSelection="options.rowSelection"
        :pagination="{ pageSize: 10 }"
        :scroll="{ y: 600, x: 1800 }"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>

        <span slot="ksrq" slot-scope="ksrq">{{ ksrq.slice(0, 10) }}</span>
      </s-table>
    </a-card>
  </div>
</template>

<script>
import { STable } from '~'
import { mapState } from 'vuex'

export default {
  name: 'single-printed',
  data() {
    return {
      form: this.$form.createForm(this),
      kcLoading: false,
      advanced: true,
      queryParam: {},
      columns: [
        {
          title: this.$t('key.no'),
          width: '80px',
          scopedSlots: { customRender: 'serial' }
        },
        {
          title: this.$t('key.kcdm'),
          width: '120px',
          dataIndex: 'kcbm'
        },
        {
          title: this.$t('key.kcmc'),
          width: '150px',
          dataIndex: 'kcmc'
        },
        {
          title: this.$t('key.kclb'),
          width: '150px',
          dataIndex: 'kclbmc'
        },
        {
          title: this.$t('key.kkyx'),
          width: '150px',
          dataIndex: 'ksyxmc'
        },
        {
          title: this.$t('key.ksxn'),
          width: '150px',
          dataIndex: 'ksxn'
        },
        {
          title: this.$t('key.ksnj'),
          width: '150px',
          dataIndex: 'ksnj'
        },
        {
          title: this.$t('key.ksxq'),
          width: '120px',
          dataIndex: 'ksxqmc'
        },
        {
          title: this.$t('key.kslb'),
          width: '120px',
          dataIndex: 'kslbmc'
        },
        {
          title: this.$t('key.ksxz'),
          width: '120px',
          dataIndex: 'ksxzmc'
        },
        {
          title: this.$t('key.ksrq'),
          width: '150px',
          dataIndex: 'ksrq',
          scopedSlots: { customRender: 'ksrq' }
        },
      ],
      loadData: parameter => {
        parameter.pageNum = parameter.pageNo
        let params = Object.assign(this.queryParam, parameter)
        return this.$api.cultivate.yfbList(params).then(res => {
          console.log(res)
          return {
            data: res.data.rows,
            pageNo: parameter.pageNo,
            pageSize: parameter.pageSize,
            totalPage: Math.ceil(res.data.total / parameter.pageSize),
            totalCount: res.data.total
          }
        })
      },
      selectedRowKeys: [],
      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      selectMenu: {}
    }
  },
  components: { STable },
  computed: {
    ...mapState({
      TYPE0125: state => state.app['TYPE0125'], // 学年
      TYPE0072: state => state.app['TYPE0072'], // 学期
      TYPE0109: state => state.app['TYPE0109'], // 考试类别
      TYPE0359: state => state.app['TYPE0359'], // 课程代码
      TYPE0358: state => state.app['TYPE0358'], // 考试性质
    })
  },
  created () {
    this.$store.dispatch('app/setDictionary', 'TYPE0125')
    this.$store.dispatch('app/setDictionary', 'TYPE0072')
    this.$store.dispatch('app/setDictionary', 'TYPE0109')
    this.$store.dispatch('app/setDictionary', 'TYPE0359')
    this.$store.dispatch('app/setDictionary', 'TYPE0358')
    this.initSelectMenu()
  },
  methods: {
    initSelectMenu () {
      let allRequest = [
        this.$api.base.collegeAll()
      ]
      Promise.all(allRequest).then(res => {
        this.selectMenu = {
          yx: res[0].data
        }
      })
    },
    filterOption (value, op) {
      return op.componentOptions.children[0].text.indexOf(value) !== -1
    },
    yxChange (code) {
      this.$nextTick(() => {
        this.$set(this.queryParam, 'kcdm', '')
        this.form.setFieldsValue({kcbm: ''})
        this.$set(this.selectMenu, 'courseList', [])
        if (code) {
          this.kcLoading = true
          this.$api.cultivate.courseAll({kcssyxm: code}).then(res => {
            this.$set(this.selectMenu, 'courseList', res.data)
          }).finally(() => {
            this.kcLoading = false
          })
        } else {
          this.$set(this.selectMenu, 'courseList', [])
        }
      })
    },
    kcChange (kch) {
      this.$nextTick(() => {
        let kc = this.selectMenu.courseList.find(kc => {
          return kc.kch === kch
        })
        this.form.setFieldsValue({
          xs: kc.zxs,
          xf: kc.xf
        })
      })
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    handleQuery() {
      this.$refs.table.refresh(true)
      this.resetSelect()
    },
    resetSelect() {
      this.$nextTick(() => {
        this.$refs.table.selectedRows = []
        this.$refs.table.selectedRowKeys = []
      })
    },
    handlePrint() {
      if (!this.selectedRows) return this.$message.info('请选择要打印的课程！');
      let ids = this.selectedRows.map(o => o.id).toString()
      console.log(this.selectedRows)
      console.log({ids})
      this.$downloadPdf('/teacherClient/kccj/dkcjPrint', { ids }, '单科成绩.pdf')
      this.resetSelect()
      
    },
    handlePrint2() {
      if (!this.selectedRows) return this.$message.info('请选择要合并打印的课程！');
      let ids = this.selectedRows.map(o => o.id).toString()
      this.$downloadPdf('/teacherClient/kccj/dkcjPrintM', { ids }, '单科成绩.pdf')
      this.resetSelect()
      
    }
  }
}
</script>

